package com.jacobson.huobi.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.text.TextUtils;


import com.jacobson.huobi.R;

import java.util.Objects;

/**
 * 系统设置
 *
 * @author WuChenZhong on 2019/7/6 23:29
 * @version 1.0.0
 */
public class ConfigurationProperties {
    private static final String IP = "ip";
    private static final String PORT = "port";
    private static final String IS_MUTE = "is_mute";
    private static final String AUTH_CODE = "auth_code";
    private static final String TWINKLING_TIME = "twinkling_time";
    private static final String KEY_TABS_MAPPING = "tabs_mapping";
    private static final String KEY_COLORS_MAPPING = "colors_mapping";
    private static final String USE_CUSTOM_VOLUME = "is_use_custom_volume";
    private static final String RINGTONE_SELL = "ringtone_sell";
    private static final String RINGTONE_BUY = "ringtone_buy";
    private static final String RINGTONE_DEFAULT = "ringtone_default";
    private Context context = null;
    private SharedPreferences preferences;

    public void init(Context context) {
        this.context = context;
        preferences = PreferenceManager.getDefaultSharedPreferences(context);
    }


    public String getIp() {
        return preferences.getString(IP, "");
    }

    public int getPort() {
        return Integer.parseInt(Objects.requireNonNull(preferences.getString(PORT, "8369")));
    }

    public int getTwinklingTime() {
        return Integer.parseInt(Objects.requireNonNull(preferences.getString(TWINKLING_TIME, "300")));
    }

    public boolean isMute() {
        return preferences.getBoolean(IS_MUTE, false);
    }

    public String getAuthCode() {
        return Objects.requireNonNull(preferences.getString(AUTH_CODE, "")).toUpperCase();
    }

    private volatile static ConfigurationProperties sInstance;

    private ConfigurationProperties() {
    }

    public static ConfigurationProperties getInstance() {
        if (sInstance == null) {
            synchronized (ConfigurationProperties.class) {
                if (sInstance == null) {
                    sInstance = new ConfigurationProperties();
                }
            }
        }
        return sInstance;
    }

    public String getRingtoneKey(int ringType) {
        if (ringType == 2) {
            return RINGTONE_SELL;
        }else if (ringType == 1) {
            return RINGTONE_BUY;
        }else {
            return RINGTONE_DEFAULT;
        }
    }

    public String getRingtone(int ringType) {
        if (ringType == 2) {
            return preferences.getString(RINGTONE_SELL, "android.resource://" + this.context.getPackageName() +"/raw/" + R.raw.buy);

        }else if (ringType == 1) {
            return preferences.getString(RINGTONE_BUY, "android.resource://" + this.context.getPackageName() +"/raw/" + R.raw.sell);

        }else {
            return preferences.getString(RINGTONE_DEFAULT, "android.resource://" + this.context.getPackageName() +"/raw/" + R.raw.alarm);
        }
    }

    public void setRingtone(int ringType, String ringValue) {
        if (ringType == 1) {
            preferences.edit().putString(RINGTONE_SELL, ringValue).commit();
        }else if (ringType == 2) {
            preferences.edit().putString(RINGTONE_BUY, ringValue).commit();
        }else {
            preferences.edit().putString(RINGTONE_DEFAULT, ringValue).commit();
        }
    }

    public float getRingtoneVolume() {
        return Float.valueOf(Objects.requireNonNull(preferences.getString("ringtone_volume", "0.5")));
    }

    public boolean isUseCustomVolume() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
            // 该设置仅支持SDK_INT大于P以上
            return false;
        }
        return preferences.getBoolean(USE_CUSTOM_VOLUME, false);
    }

    public String getRealColor(String colorSymbol) {
//        1、红线　2、淡黄色　3、淡绿色　4、淡灰色　5、淡粉色　6、无色
        String str = preferences.getString(KEY_COLORS_MAPPING, "1=#FF0000,2=#CCCC33,3=#66FF66,4=#CCCCCC,5=#FF99FF,6=#00000000");
        if (TextUtils.isEmpty(str)) {
            return "#00000000";
        }
        try {
            String[] split = str.split(",");
            for (String s : split) {
                String[] split1 = s.split("=");
                if (split1.length == 2 && split1[0].equals(colorSymbol)) {
                    return split1[1];
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "#00000000";
    }
}
